package com.ruoyi.supply.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.store.domain.Commodity;
import com.ruoyi.supply.domain.PurchaseOrderItem;
import com.ruoyi.supply.dto.PurchaseOrderDto;
import com.ruoyi.supply.mapper.PurchaseOrderItemMapper;
import com.ruoyi.supply.util.CodeGenerateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supply.mapper.PurchaseOrderMapper;
import com.ruoyi.supply.domain.PurchaseOrder;
import com.ruoyi.supply.service.IPurchaseOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author kai
 * @date 2022-08-24
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService 
{
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Autowired
    private PurchaseOrderItemMapper purchaseOrderItemMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param purchaseOrder 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param purchaseOrderDto 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPurchaseOrder(PurchaseOrderDto purchaseOrderDto)
    {
        String orderNo = CodeGenerateUtils.generateOrderSn(6L);

        //生成订单对象
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setOrderNo(orderNo);
        purchaseOrder.setTotalAmount(purchaseOrderDto.getTotalAmount());
        purchaseOrder.setDiscountAmount(purchaseOrderDto.discountAmount);
        purchaseOrder.setAfterDiscountAmount(purchaseOrderDto.getTotalAmount());
        purchaseOrder.setEarnest(purchaseOrderDto.getDepositAmount());
        purchaseOrder.setSettlementAccountSn(purchaseOrderDto.getAccountId().toString());
        purchaseOrder.setSupplierId(purchaseOrderDto.getSupplierId());

        purchaseOrder.setCreatTime(new Date());
        purchaseOrder.setCommodityInformation("机器");
        purchaseOrder.setOperator("张三");
        purchaseOrder.setState(0);
        purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);

        for(Commodity item : purchaseOrderDto.getSelectGoodsList()){
            PurchaseOrderItem purchaseOrderItem = new PurchaseOrderItem();
            BeanUtils.copyProperties(item,purchaseOrderItem);
            purchaseOrderItem.setSize(item.getSpecification());
            purchaseOrderItem.setPrice(item.getPurchasePrice());
            purchaseOrderItem.setAmount(item.getNumber());
            purchaseOrderItem.setOrderId(purchaseOrder.getId());
            purchaseOrderItem.setOrderNo(orderNo);
            purchaseOrderItem.setSupplier(purchaseOrderDto.getSupplierId().toString());
            purchaseOrderItemMapper.insertPurchaseOrderItem(purchaseOrderItem);
        }

        return 1;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param purchaseOrder 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderByIds(Long[] ids)
    {
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }

    /**
     * 审核
     * @param purchaseOrder
     * @return
     */
    @Override
    public int auditPurchaseOrderById(PurchaseOrder purchaseOrder) {
        purchaseOrder.setState(1);
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }
}
